﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Raona.Silverlight;

namespace RaonaShowcase
{
	public partial class Showcase : UserControl
	{
		private int flipcount=0;
		
		public Showcase()
		{
			// Required to initialize variables
			InitializeComponent();
			
			// General Variables
			
			int LayoutWidth= Convert.ToInt32(ConfigurationManager.AppSettings["LayoutWidth"]);
			int LayoutHeight= Convert.ToInt32(ConfigurationManager.AppSettings["LayoutHeight"]);
			int LayoutColumns= Convert.ToInt32(ConfigurationManager.AppSettings["LayoutColumns"]);
			int LayoutRows= Convert.ToInt32(ConfigurationManager.AppSettings["LayoutRows"]);
			int FlipWidth= Convert.ToInt32(ConfigurationManager.AppSettings["FlipWidth"]);
			int FlipHeight= Convert.ToInt32(ConfigurationManager.AppSettings["FlipHeight"]);

									
			// Set Config Parameters
			
			this.grdLayoutRoot.Width = LayoutWidth; 
			this.grdLayoutRoot.Height= LayoutHeight;

			for(int i=0;i<LayoutColumns;i++)
			{
			    var column = new ColumnDefinition();
			    column.Width= new GridLength(Convert.ToDouble(LayoutWidth/LayoutColumns));	
			    this.grdLayoutRoot.ColumnDefinitions.Add(column);
			}	
			
			for(int i=0;i<LayoutRows;i++)
			{
			    var row = new RowDefinition();
			    row.Height= new GridLength(Convert.ToDouble(LayoutHeight/LayoutRows));	
			    this.grdLayoutRoot.RowDefinitions.Add(row);
			}	
			
			// FlipElement creation & fill
			
			var colPosition = 0;
			var rowPosition = 0;

			foreach (var tag in TagManager.TagList)
			{
				
			FlipElement flip = new FlipElement();

            flip.Title = tag["title"];
			flip.FrontImgPath= tag["frontimgpath"]; 
			flip.BackImgPath= tag["backimgpath"];
			flip.LogoImgPath= tag["logoimgpath"];
			flip.Slogan= tag["slogan"];
			flip.Description= tag["description"];
			flip.Tag1= tag["tag1"];
			flip.Tag2= tag["tag2"];
			flip.Tag3= tag["tag3"];
			flip.UrlLink= tag["urllink"];
	
			this.grdLayoutRoot.Children.Add(flip);

			flip.Width = FlipWidth;
			flip.Height =FlipHeight;
				
			// Calculating column & row position into grid.
				
			flip.SetValue(Grid.ColumnProperty,colPosition);
			flip.SetValue(Grid.RowProperty,rowPosition);
				
			colPosition++;
				
			if (colPosition==LayoutColumns)	
			{
				rowPosition++;
				colPosition=0;
			}
			
			flip.FlipLoaded +=new System.EventHandler<EventArgs>(AllFlipsLoaded);
			flipcount++;
			
			}	
			this.Cursor=Cursors.Wait;
			this.Visibility= Visibility.Collapsed;
		}

        private void AllFlipsLoaded(object sender, EventArgs e)
        {
        		flipcount--;
			
				if(flipcount==0) {
					this.Visibility= Visibility.Visible;
					this.Cursor= Cursors.Arrow;
                    ((App)Application.Current).HideLoadingControl();
				}			
        }
		
		
	}
}